const devDao = require('../dao/dev');

const Response = require('../entity/http/Response');

const devService = {
    async addArgvVersion(reqId, uid, { version, count, content, type }) {
        let result;
        try {
            let dbResult = await devDao.addArgvVersion(reqId, uid, version, count, content, type);
            if (dbResult.insertId) {
                result = new Response().success("创建参数版本成功");
            } else {
                result = new Response().fail(500, "创建参数版本失败");
            }
        } catch (err) {
            result = new Response().fail(500, "创建参数版本失败 请检查参数是否正确 参数版本是否重复");
        }
        return result;
    },
    async getAllArgvVersion(reqId, uid) {
        let result;
        try {
            let dbResult = await devDao.getAllArgvVersion(reqId, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取所有参数版本失败");
        }
        return result;
    },
    async getArgvVersionInfo(reqId, uid, { id }) {
        let result;
        try {
            let dbResult = await devDao.getArgvVersionInfo(reqId, uid, id);
            if (dbResult.length) {
                result = new Response().success(dbResult[0]);
            } else {
                result = new Response().fail(500, "不存在该参数版本的信息");
            }
        } catch (err) {
            result = new Response().fail(500, "获取所有参数版本失败");
        }
        return result;
    },
    async copyCreateArgvVersion(reqId, uid, { id, version }) {
        let result;
        try {
            let dbResult = await devDao.copyCreateArgvVersion(reqId, uid, id, version);
            if (dbResult.insertId) {
                result = new Response().success("复制创建参数版本成功");
            } else {
                result = new Response().fail(500, "复制创建参数版本失败");
            }
        } catch (err) {
            result = new Response().fail(500, "创建参数版本失败 请检查参数是否正确 参数版本是否重复");
        }
        return result;
    },
    async getArgvVersionUsed(reqId, uid, id) {
        let result;
        try {
            let dbResult = await devDao.getArgvVersionUsed(reqId, uid, id);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "查看参数版本使用情况失败");
        }
        return result;
    },
    async deleteArgvVersion(reqId, uid, id) {
        let result;
        try {
            let dbResult = await devDao.deleteArgvVersion(reqId, uid, id);
            if (dbResult.changedRows) {
                result = new Response().success("删除参数版本成功");
            } else {
                result = new Response().fail(500, "该参数版本不存在");
            }
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, err.message);
        }
        return result;
    },
    async updateArgvVersion(reqId, uid, id, { version, count, content, type }) {
        let result;
        try {
            let dbResult = await devDao.updateArgvVersion(reqId, uid, id, version, count, content, type);
            if (dbResult.changedRows) {
                result = new Response().success("修改参数版本信息成功");
            } else {
                result = new Response().fail(500, "修改前后参数版本信息无变化");
            }
        } catch (err) {
            result = new Response().fail(500, "修改参数版本信息失败");
        }
        return result;
    },
    async addDevType(reqId, uid, { type, name }) {
        let result;
        try {
            let dbResult = await devDao.addDevType(reqId, uid, type, name);
            if (dbResult.insertId) {
                result = new Response().success("创建设备类型成功");
            } else {
                result = new Response().fail(500, "创建设备类型失败");
            }
        } catch (err) {
            result = new Response().fail(500, "创建设备类型失败 请检查参数是否正确 设备类型是否重复");
        }
        return result;
    },
    async getAllDevType(reqId, uid) {
        let result;
        try {
            let dbResult = await devDao.getAllDevType(reqId, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取所有设备类型失败");
        }
        return result;
    },
    async updateDevType(reqId, uid, devTypeId, { type, name }) {
        let result;
        try {
            let dbResult = await devDao.updateDevType(reqId, uid, devTypeId, type, name);
            if (dbResult.changedRows) {
                result = new Response().success("修改设备类型成功");
            } else {
                result = new Response().fail(500, "修改设备类型失败 修改前后数据无变化");
            }
        } catch (err) {
            result = new Response().fail(500, "修改设备类型失败 请检查参数是否正确 设备类型是否重复");
        }
        return result;
    },
    async getDevTypeUsed(reqId, uid, id) {
        let result;
        try {
            let dbResult = await devDao.getDevTypeUsed(reqId, uid, id);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "查看设备类型使用情况失败");
        }
        return result;
    },
    async deleteDevType(reqId, uid, id) {
        let result;
        try {
            let dbResult = await devDao.deleteDevType(reqId, uid, id);
            if (dbResult.changedRows) {
                result = new Response().success("删除设备类型成功");
            } else {
                result = new Response().fail(500, "该设备类型不存在");
            }
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, err.message);
        }
        return result;
    },
    async addDevice(reqId, uid, { departmentId, deviceTypeId, devID, devPswd, enzymeSolution, disinfectant }) {
        let result;
        try {
            let dbResult = await devDao.addDevice(reqId, uid, departmentId, deviceTypeId, devID, devPswd, enzymeSolution, disinfectant);
            if (dbResult.insertId) {
                result = new Response().success("添加设备成功");
            } else {
                result = new Response().fail(500, "添加设备失败");
            }
        } catch (err) {
            result = new Response().fail(500, "添加设备失败 请检查设备ID是否重复");
        }
        return result;
    },
    async getDeviceList(reqId, uid) {
        let result;
        try {
            let dbResult = await devDao.getDeviceList(reqId, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "添加设备失败");
        }
        return result;
    },
    async getDeviceInfo(reqId, uid, id) {
        let result;
        try {
            let dbResult = await devDao.getDeviceInfo(reqId, uid, id);
            if (dbResult.length) {
                result = new Response().success(dbResult[0]);
            } else {
                result = new Response().fail(500, "该设备不存在");
            }
        } catch (err) {
            result = new Response().fail(500, "获取设备详细信息失败");
        }
        return result;
    },
    async deleteDevice(reqId, uid, id) {
        let result;
        try {
            let dbResult = await devDao.deleteDevice(reqId, uid, id);
            if (dbResult.changedRows) {
                result = new Response().success("删除设备成功");
            } else {
                result = new Response().fail(500, "该设备不存在");
            }
        } catch (err) {
            result = new Response().fail(500, "删除设备失败");
        }
        return result;
    },
    async getDeviceArgv(reqId, uid, id) {
        let result;
        try {
            let dbResult = await devDao.getDeviceArgv(reqId, uid, id);
            if (dbResult.length) {
                result = new Response().success(dbResult[0]);
            } else {
                result = new Response().fail(500, "该设备不存在");
            }
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "获取设备参数信息失败");
        }
        return result;
    },
    async ctrlReadArgv(reqId, uid, id) {
        let result;
        try {
            await devDao.ctrlReadArgv(reqId, uid, id);
            result = new Response().success("控制设备读取参数成功");
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "控制设备读取参数失败");
        }
        return result;
    },
    async ctrlSetStatus(reqId, uid, id, status) {
        let result;
        try {
            await devDao.ctrlSetStatus(reqId, uid, id, status);
            result = new Response().success("控制设备设置状态成功");
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "控制设备设置状态失败");
        }
        return result;
    },
    async ctrlSetPassword(reqId, uid, id, password) {
        let result;
        try {
            await devDao.ctrlSetPassword(reqId, uid, id, password);
            result = new Response().success("控制设备设置密码成功");
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "控制设备设置密码失败");
        }
        return result;
    },
    async ctrlCancel(reqId, uid, id) {
        let result;
        try {
            await devDao.ctrlCancel(reqId, uid, id);
            result = new Response().success("控制设备取消当前操作成功");
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "控制设备取消当前操作失败");
        }
        return result;
    },
    async updateDevDepartment(reqId, uid, id, departmentId) {
        let result;
        try {
            let dbResult = await devDao.updateDevDepartment(reqId, uid, id, departmentId);
            if (dbResult.changedRows) {
                result = new Response().success("修改设备归属科室成功");
            } else {
                result = new Response().fail(500, "修改前后数据无变化");
            }
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "修改设备归属科室失败");
        }
        return result;
    },
    async updateDevInfo(reqId, uid, { id, enzymeSolution, disinfectant, warrantyStartDate, warrantyEndDate }) {
        let result;
        try {
            let dbResult = await devDao.updateDevInfo(reqId, uid, id, enzymeSolution, disinfectant, warrantyStartDate, warrantyEndDate);
            if (dbResult.changedRows) {
                result = new Response().success("修改设备医疗参数成功");
            } else {
                result = new Response().fail(500, "修改前后数据无变化");
            }
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "修改设备医疗参数失败");
        }
        return result;
    },
    async updateOpenRate(reqId, uid, { id, openRate }) {
        let result;
        try {
            let dbResult = await devDao.updateDevOpenRate(reqId, uid, id, openRate);
            if (dbResult.changedRows) {
                result = new Response().success("修改设备开机率成功");
            } else {
                result = new Response().fail(500, "修改前后数据无变化");
            }
        } catch (err) {
            console.log(err);
            result = new Response().fail(500, "修改设备开机率失败");
        }
        return result;
    }
};

module.exports = devService;